/**
 * Project SQLiteWrapper
 */


#include <iostream>
#include "ContentValues.h"
#include "Utils.h"

using std::make_pair;

/**
 * ContentValues implementation
 */


/**
 * @param key
 * @param value
 * @return void
 */
void ContentValues::PutBool(const string& key, const bool& value) {
    values_.insert(make_pair(key, to_string(value)));
}

/**
 * @param key
 * @param value
 * @return void
 */
void ContentValues::PutInt32(const string& key, const int32_t& value) {
    values_.insert(make_pair(key, to_string(value)));
}

/**
 * @param key
 * @param value
 * @return void
 */
void ContentValues::PutInt64(const string& key, const int64_t& value) {
    values_.insert(make_pair(key, to_string(value)));
}

/**
 * @param key
 * @param value
 * @return void
 */
void ContentValues::PutText(const string& key, const string& value) {
    values_.insert(make_pair(key, toSQLString(value)));
}

/**
 * @param key
 * @return void
 */
void ContentValues::PutNULL(const string& key) {
    values_.insert(make_pair(key, "NULL"));
}

string ContentValues::GetInsertString() {
    string column_names;
    string values;
    map<string, string>::iterator it;
    for (it = values_.begin(); it != values_.end(); ++it) {
        column_names += it->first;
        column_names += ", ";
        values += it->second;
        values += ", ";
    }
    column_names = column_names.substr(0, column_names.length() - 2);
    values = values.substr(0, values.length() - 2);

    return "(" + column_names + ")" + " VALUES " + "(" + values + ")";
}

string ContentValues::GetUpdateString() {
    string column_name;
    string value;
    string sql;
    map<string, string>::iterator it;
    for (it = values_.begin(); it != values_.end(); ++it) {
        column_name = it->first;
        value = it->second;
        sql += column_name + " = " + value + ", ";
    }

    sql = sql.substr(0, sql.length() - 2);

    return sql;
}

void ContentValues::Clear() {
    values_.clear();
}
